package com.shadt.cloud.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shadt.cloud.dao.BranchDao;
import com.shadt.cloud.entity.CloudBranch;
import com.shadt.cloud.entity.User;
import com.shadt.cloud.service.BranchService;
import com.shadt.cloud.vo.BranchVo;
import com.shadt.core.util.DateUtil;
import com.shadt.core.util.RandomStringGenerator;

/**
 * 分平台业务逻辑层实现类
 * 
 * @author SongR
 *
 */
@Service
public class BranchServiceImpl implements BranchService {

	@Autowired
	BranchDao dao;

	/**
	 * 实现验证分平台是否已注册
	 */
	public BranchVo checkBranch(String appname)  throws Exception{
		CloudBranch b = dao.get("from CloudBranch where name ='" + appname + "' and status !=-1");
		BranchVo vo = new BranchVo();
		if (null != b ) {
			BeanUtils.copyProperties(b, vo);
			return vo;
		} else {
			return null;
		}
	}

	/**
	 * 根据分平台名称获取分平台信息
	 */
	public BranchVo getBranch(String appname) throws Exception{
		CloudBranch b = dao.getR(appname);
		if(null==b){
			b = dao.get("from CloudBranch where name ='" + appname + "'");
		}
		BranchVo vo = new BranchVo();
		if (null != b && b.getStatus() != -1) {
			BeanUtils.copyProperties(b, vo);
		} else {
			return null;
		}
		return vo;
	}

	/**
	 * 实现分平台注册
	 */
	public BranchVo register(String appname, String username, String phone,String password) throws Exception{
		CloudBranch b = dao.get("from CloudBranch where name ='" + appname + "'");
		BranchVo vo = new BranchVo();
		if (null == b) {
			b = new CloudBranch();
			b.setName(appname);
			b.setUsername(username);
			b.setUserphone(phone);
			b.setPassword(password);
			b.setSecret(RandomStringGenerator.getRandomStringByLength(32));
			b.setStatus(1);
			b.setCreateTime(new Date());
			dao.save(b);
			BeanUtils.copyProperties(b, vo);
		}else if(b.getStatus()==-1){
			b.setStatus(1);
			b.setCreateTime(new Date());
			b.setPassword(password);
			b.setSecret(RandomStringGenerator.getRandomStringByLength(32));
			dao.update(b);
			BeanUtils.copyProperties(b, vo);
		}else{
			vo.setName(b.getName());
			vo.setUsername(b.getUsername());
			vo.setUserphone(b.getUserphone());
			vo.setPassword(b.getPassword());
		}
		return vo;
	}

	/**
	 * 实现获取当前用户的所有平台
	 */
	public List<BranchVo> getBranch(String search, Integer start, Integer length, User u) throws Exception{
		String hql = "from CloudBranch where status !=-1";
		if(!search.equals("")){
			hql+=" and name like '%"+search+"%'";
		}
		if(u.getType()==1){
			hql +=" and userId="+u.getId();
		}
		hql+=" order by createTime,status desc";
		List<CloudBranch> branch=dao.findByPage(hql, start, length);
		List<BranchVo> vos = new ArrayList<BranchVo>();
		for (CloudBranch b : branch) {
			BranchVo vo = new BranchVo();
			BeanUtils.copyProperties(b, vo);
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 实现获取当前用户的所有平台数量
	 */
	public Object getBranchCount(String search, User u) throws Exception{
		String hql = "select count(*) from CloudBranch where status !=-1";
		if(!search.equals("")){
			hql+=" and name like '%"+search+"%'";
		}
		if(u.getType()==1){
			hql +=" and userId="+u.getId();
		}
		Object o = dao.count(hql);
		return o;
	}

	/**
	 * 实现添加平台
	 */
	public BranchVo add(User u, CloudBranch branch) throws Exception{
		branch.setUserId(u.getId());
		branch.setUsername(u.getName());
		branch.setCreateTime(new Date());
		branch.setStatus(2);
		branch.setSecret(RandomStringGenerator.getRandomStringByLength(32));
		dao.save(branch);
		BranchVo vo = new BranchVo();
		BeanUtils.copyProperties(branch, vo);
		return vo;
	}

	/**
	 * 实现编辑平台名称
	 */
	public void editBranch(String id, String value) throws Exception {
		CloudBranch branch = dao.get(CloudBranch.class,new Long(id));
		branch.setName(value);
		dao.update(branch);
	}

	/**
	 * 实现删除平台（逻辑删除）
	 */
	public void delBranch(Long id) throws Exception {
		CloudBranch branch = dao.get(CloudBranch.class,id);
		branch.setStatus(-1);
		dao.update(branch);
		
	}

	@Override
	public List<BranchVo> getBranch(String search, Integer start, Integer length, User u, String type)
			throws Exception {
		String hql = "from CloudBranch where status ="+type;
		if(!search.equals("")){
			hql+=" and name like '%"+search+"%'";
		}
		hql+=" order by createTime,status";
		List<CloudBranch> branch=dao.findByPage(hql,start,length);
		List<BranchVo> vos = new ArrayList<BranchVo>();
		for (CloudBranch b : branch) {
			BranchVo vo = new BranchVo();
			BeanUtils.copyProperties(b, vo);
			vo.setUsername(u.getName());
			vos.add(vo);
		}
		return vos;
	}

	@Override
	public Object getBranchCount(String search, User u, String type) throws Exception {
		String hql = "select count(*) from CloudBranch where status ="+type;
		if(!search.equals("")){
			hql+=" and name like '%"+search+"%'";
		}
		Object o = dao.count(hql);
		return o;
	}

	/**
	 * 实现停用平台
	 */
	public void banBranch(Long id) throws Exception{
		CloudBranch branch = dao.get(CloudBranch.class,id);
		branch.setStatus(1);
		dao.update(branch);
	}

	//可用
	public void recoveryBranch(Long id) throws Exception {
		CloudBranch branch = dao.get(CloudBranch.class,id);
		branch.setStatus(0);
		dao.update(branch);
	}

	//审核中
	public void reloadBranch(Long id) {
		CloudBranch branch = dao.get(CloudBranch.class,id);
		branch.setStatus(2);
		dao.update(branch);
	}

	@Override
	public void setState(Long id, int i) {
		CloudBranch branch = dao.get(CloudBranch.class,id);
		branch.setStatus(i);
		dao.update(branch);
	}

	@Override
	public String setSecret(Long id) throws Exception {
		String str = RandomStringGenerator.getRandomStringByLength(32);
		CloudBranch branch = dao.get(CloudBranch.class,id);
		branch.setSecret(str);
		dao.update(branch);
		return str;
	}

	@Override
	public void editsBranch(String id, String value) throws Exception {
		CloudBranch branch = dao.get(CloudBranch.class,new Long(id));
		branch.setStartTime(DateUtil.strToDate(value, "yyyy-MM-dd HH:mm:ss"));
		dao.update(branch);
	}

}
